package com.ivan.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ivan.common.utils.MD5;
import com.ivan.model.entity.hosp.HospitalSet;
import com.ivan.hospital.mapper.HospitalSetMapper;
import com.ivan.hospital.service.HospitalSetService;
import com.ivan.model.vo.hosp.HospitalSetQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * @author Ivan
 * @date 2021/3/13 18:45
 * @Description:
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    //mybatis-plus自动完成了注入
//    @Autowired
//    HospitalSetMapper hospitalSetMapper;


    /**
     * 条件查询带分页
     * @param current
     * @param limit
     * @param queryVo
     * @return
     */
    @Override
    public Page<HospitalSet> findByPage(long current, long limit, HospitalSetQueryVo queryVo) {
        //分页对象
        Page<HospitalSet> page = new Page<>(current, limit);
        //查询条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = queryVo.getHosname();//医院名称
        String hoscode = queryVo.getHoscode();//医院编号
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", queryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", queryVo.getHoscode());
        }
        return baseMapper.selectPage(page, wrapper);
    }

    /**
     * 添加医院设置
     * @param hospitalSet
     * @return
     */
    @Override
    public int saveHospitalSet(HospitalSet hospitalSet) {
        //设置状态 1可用  0不可用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        return baseMapper.insert(hospitalSet);
    }

    /**
     * 锁定/解锁医院设置
     * @param id
     * @param status
     * @return
     */
    @Override
    public int lockHospitalSet(Long id, Integer status) {
        //根据id查询医院设置数据
        HospitalSet hospitalSet = baseMapper.selectById(id);
        //设置锁定/解锁状态
        hospitalSet.setStatus(status);
        return baseMapper.updateById(hospitalSet);
    }
}
